#include "ClientInfo.h"
#include "Util.h"
ClientInfo::ClientInfo(int socketType)
{
	if (socketType == ClientInfo::SOCKET_SERVER) 
	{
		initSocketServer();
	} else 
	{
		initSocketClient();
	}
}

ClientInfo::~ClientInfo(void)
{
	closesocket(mSocket);
}
int ClientInfo::initSocketServer() 
{
	WSADATA wsaData;
	mSocket = INVALID_SOCKET;
	SOCKET ListenSocket = INVALID_SOCKET;    
    result = NULL;
    struct addrinfo                hints;
    int iResult;
    

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
		
        //printf("WSAStartup failed: %d\n", iResult);
		//lastError = "WSAStartup failed";
		return 1;
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve the server address and port
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {        
        WSACleanup();
		//lastError = "getaddrinfo failed";
		return 1;
    }

    // Create a SOCKET for connecting to server
	ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
	if (ListenSocket == INVALID_SOCKET) {
        //printf("socket failed: %ld\n", WSAGetLastError());
		MessageBoxA(0,"1","Error",0);
        freeaddrinfo(result);
        WSACleanup();
        //lastError = "can't create listen socket";
		return 1;
    }

	iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        //printf("bind failed with error: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }


	freeaddrinfo(result);

    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

	mSocket = accept(ListenSocket, NULL, NULL);
	if (mSocket == INVALID_SOCKET) {
        printf("accept failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }
	closesocket(ListenSocket);

	return 0;
}


int ClientInfo::initSocketClient()
{
	mSocket = INVALID_SOCKET;
	SOCKET lhSocket;
	//MessageBoxA(0,"asd","as",0);

	SOCKADDR_IN lSockAddr;
	WSADATA wsaData;
	int lConnect;
	if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
	{
	   Util::log("Socket Initialization Error. Program aborted\n");
		return 1;
	}
	lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(lhSocket == INVALID_SOCKET)
	{
	   Util::log("Invalid Socket \n");
	}
	memset(&lSockAddr,0, sizeof(lSockAddr));
	lSockAddr.sin_family = AF_INET;
	lSockAddr.sin_port = htons(7787);
	lSockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	//lSockAddr.sin_addr.s_addr = inet_addr("10.10.0.77");
	//lSockAddr.sin_addr.s_addr=INADDR_ANY;	
	lConnect = connect(lhSocket,(SOCKADDR *)&lSockAddr,sizeof(SOCKADDR_IN));
	if(lConnect != 0)
	{
	   Util::log("Connect Error. Program aborted\n");
		 return 1;
	}
	mSocket = lhSocket;
	return 0;
}
